# !/bin/bash
# web_site_url_list_discovery.sh
# yinqinghong.cn 201504201330

INFO_FILE=/etc/zabbix/scripts/web_site_url_list

###command info
AGENT='monitor_jyt'
CURL="curl -sL --connect-timeout 15 -m 15 -A $AGENT "

web_site_url_list_discovery () {

cat $INFO_FILE | grep -v '^#' | grep '::.*::'  |awk -F'::' 'BEGIN {print "{\n\t\"data\":[\n\t"}  \
{if(NF<7) { \
           DOMAIN=$1; \
           sub(/\/.*/,"",DOMAIN); \
           print "\t{\n\t\t\"{#STEPNAME}\":""\""NR"\","; \
           print "\t\t\"{#SERVER}\":\""DOMAIN"\"," ; \
           print "\t\t\"{#DOMAIN}\":\""DOMAIN"\","; \
           print "\t\t\"{#URL}\":\""$1"\","; \
           print "\t\t\"{#REQPOST}\":\""$2"\","; \
           print "\t\t\"{#REQVAR}\":\""$3"\","; \
           print "\t\t\"{#REQHEADER}\":\""$4"\","; \
           print "\t\t\"{#REQSTR}\":\""$5"\","; \
           print "\t\t\"{#REQCODES}\":\""$6"\"},"; \
          }else{ \
                   for(i=7;i<=NF;i++) { \
                       DOMAIN=$1; \
                       sub(/\/.*/,"",DOMAIN); \
                       if(DOMAIN == NUL){ DOMAIN=$i;URL=$i$1; } else { URL=$1;}\
                       print "\t{\n\t\t\"{#STEPNAME}\":""\""NR,i"\","; \
                       print "\t\t\"{#SERVER}\":\""$i"\"," ; \
                       print "\t\t\"{#DOMAIN}\":\""DOMAIN"\","; \
                       print "\t\t\"{#URL}\":\""URL"\","; \
                       print "\t\t\"{#REQPOST}\":\""$2"\","; \
                       print "\t\t\"{#REQVAR}\":\""$3"\","; \
                       print "\t\t\"{#REQHEADER}\":\""$4"\","; \
                       print "\t\t\"{#REQSTR}\":\""$5"\","; \
                       print "\t\t\"{#REQCODES}\":\""$6"\"},"; \
                   } \
               } \
} \
END {  \

      DOMAIN=$1; \
      sub(/\/.*/,"",DOMAIN); \
      if(DOMAIN == NUL){ DOMAIN=$NF; URL=$NF; }else {URL=$1;} \
      print "\t{\n\t\t\"{#STEPNAME}\":""\"END "NR"\","; \
      print "\t\t\"{#SERVER}\":\"END"DOMAIN"\"," ; \
      print "\t\t\"{#DOMAIN}\":\"END"DOMAIN"\","; \
      print "\t\t\"{#URL}\":\"END"URL"\","; \
      print "\t\t\"{#REQPOST}\":\""$2"\","; \
      print "\t\t\"{#REQVAR}\":\""$3"\","; \
      print "\t\t\"{#REQHEADER}\":\""$4"\","; \
      print "\t\t\"{#REQSTR}\":\""$5"\","; \
      print "\t\t\"{#REQCODES}\":\""$6"\"}"; \
print "\t]\n}";}'

}

web_site_status () {

param=(
time_total
time_namelookup
time_connect
time_pretransfer
time_redirect
time_starttransfer
http_code
size_download
size_upload
size_header
size_request
speed_download
speed_upload
)

if [ $# -ge 2 ] ; then
for parameter in ${param[@]}
do
    if [ $parameter == "$2" ] ; then
       $CURL -o /dev/null -s -w %{$2} http://$1
    fi
done
fi

}

web_site_res () {

    [[ $# -ge 2 ]]&&($CURL  http://$1 2>/dev/null | grep -lqsw $2) || echo $?
}

web_site_jyt () {

    [[ $# -ge 1 ]]&& variable=`$CURL  http://$1 2>/dev/null`
     if [[ $variable =~ ^[0-9]+$ ]] ; then
           echo $variable;
           else
           echo 0;
     fi

#    [[ $# -ge 2 ]]&&($CURL  http://$1 2>/dev/null | grep -lqsw $2) || echo $?
}

case "$1" in
    web_site_url_list_discovery)
        web_site_url_list_discovery
        ;;

    web_site_status)
        web_site_status $2 $3
        ;;

    web_site_res)
        web_site_res $2 $3
        ;;

    web_site_jyt)
        web_site_jyt $2 $3
        ;;

    *)
        echo "Usage:$0 {web_site_url_list_discovery|web_site_status [URL] [parameters]|web_site_res [URL] [ResponseString]|web_site_jyt  [URL] [ResponseString]}"
        ;;
esac
